#!/bin/bash

# WWJCloud Docker 自动测试脚本
# ========================================

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# 测试计数器
TESTS_PASSED=0
TESTS_FAILED=0
TOTAL_TESTS=0

# 测试函数
run_test() {
    local test_name="$1"
    local test_command="$2"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    log_info "测试 $TOTAL_TESTS: $test_name"
    
    if eval "$test_command" >/dev/null 2>&1; then
        log_success "✅ $test_name - 通过"
        TESTS_PASSED=$((TESTS_PASSED + 1))
        return 0
    else
        log_error "❌ $test_name - 失败"
        TESTS_FAILED=$((TESTS_FAILED + 1))
        return 1
    fi
}

# 主测试函数
main() {
    echo "🚀 WWJCloud Docker 自动测试开始..."
    echo "========================================"
    
    # 1. 检查容器状态
    log_info "检查容器运行状态..."
    run_test "MySQL 容器运行" "docker ps | grep wwjcloud-mysql | grep Up"
    run_test "Redis 容器运行" "docker ps | grep wwjcloud-redis | grep Up"
    
    # 2. 等待服务就绪
    log_info "等待服务完全启动..."
    sleep 10
    
    # 3. 测试 MySQL 连接
    log_info "测试 MySQL 数据库连接..."
    run_test "MySQL Root 用户连接" "docker exec wwjcloud-mysql mysql -u root -pwwjcloud -e 'SELECT 1;'"
    run_test "MySQL wwjcloud 用户连接" "docker exec wwjcloud-mysql mysql -u wwjcloud -pwwjcloud -e 'SELECT 1;'"
    run_test "MySQL 数据库存在" "docker exec wwjcloud-mysql mysql -u wwjcloud -pwwjcloud -e 'USE wwjcloud; SHOW TABLES;'"
    
    # 4. 测试 Redis 连接
    log_info "测试 Redis 缓存连接..."
    run_test "Redis 连接测试" "docker exec wwjcloud-redis redis-cli -a redis123456 ping"
    run_test "Redis 读写测试" "docker exec wwjcloud-redis redis-cli -a redis123456 set test_key 'test_value' && docker exec wwjcloud-redis redis-cli -a redis123456 get test_key"
    
    # 5. 测试网络连通性
    log_info "测试网络连通性..."
    run_test "MySQL 端口访问" "nc -z localhost 3306"
    run_test "Redis 端口访问" "nc -z localhost 6379"
    
    # 6. 测试健康检查
    log_info "测试服务健康状态..."
    run_test "MySQL 健康检查" "docker exec wwjcloud-mysql mysqladmin ping -h localhost -u root -pwwjcloud"
    run_test "Redis 健康检查" "docker exec wwjcloud-redis redis-cli -a redis123456 ping"
    
    # 7. 测试数据持久化
    log_info "测试数据持久化..."
    run_test "MySQL 数据写入" "docker exec wwjcloud-mysql mysql -u wwjcloud -pwwjcloud -e 'USE wwjcloud; CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY, name VARCHAR(50));'"
    run_test "MySQL 数据读取" "docker exec wwjcloud-mysql mysql -u wwjcloud -pwwjcloud -e 'USE wwjcloud; INSERT INTO test_table VALUES (2, \"test2\") ON DUPLICATE KEY UPDATE name=\"test2\"; SELECT * FROM test_table;'"
    
    # 输出测试结果
    echo ""
    echo "========================================"
    echo "📊 测试结果汇总"
    echo "========================================"
    echo "总测试数: $TOTAL_TESTS"
    echo "通过: $TESTS_PASSED"
    echo "失败: $TESTS_FAILED"
    
    if [ $TESTS_FAILED -eq 0 ]; then
        log_success "🎉 所有测试通过！WWJCloud Docker 环境运行正常"
        echo ""
        echo "🌐 访问信息:"
        echo "MySQL: localhost:3306 (用户: wwjcloud, 密码: wwjcloud, 数据库: wwjcloud)"
        echo "Redis: localhost:6379 (密码: redis123456)"
        exit 0
    else
        log_error "❌ 有 $TESTS_FAILED 个测试失败，请检查服务状态"
        exit 1
    fi
}

# 运行主函数
main "$@"
